<?php

namespace App\Http\Controllers\School;

use App\Http\Requests\School\EditSchoolAdmin;
use App\Http\Requests\School\StoreSchoolAdmin;
use App\Http\Resources\School\SchoolAdminResource;
use App\Models\SchoolAdmin;
use App\Models\SchoolAdminLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;

/**
 * Class SchoolAdminController
 * @package App\Http\Controllers\School
 * @author zjw
 * 学校后台管理员
 */
class SchoolAdminController extends BaseController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     * 学校后台管理员列表
     */
    public function index(Request $request)
    {
        $schoolAdmins = SchoolAdmin::orderBy('created_at', 'desc')
            ->with(["school", "roles"])
            ->schoolIdIs(\Auth::user()->school_id)
            ->phoneLike($request->phone)
            ->paginate($request->size);
        return SchoolAdminResource::collection($schoolAdmins);
    }

    /**
     * @param StoreSchoolAdmin $request
     * @return SchoolAdminResource|array
     * 新增学校后台管理员
     */
    public function store(StoreSchoolAdmin $request)
    {
        $schoolAdmin = new SchoolAdmin();
        $schoolAdmin->fill($request->all());
        $schoolAdmin->password = $request->password;
        $schoolAdmin->school_id = \Auth::user()->school_id;
        $schoolAdmin->save();
        $roles = Role::whereIn('id', $request->roles)->get();
        $schoolAdmin->syncRoles($roles);
        SchoolAdminLog::Log("新增管理员: " . $schoolAdmin->phone);
        return new SchoolAdminResource($schoolAdmin);
    }

    /**
     * @param EditSchoolAdmin $request
     * @param SchoolAdmin $schoolAdmin
     * @return SchoolAdminResource
     */
    public function update(EditSchoolAdmin $request, SchoolAdmin $schoolAdmin)
    {
        $schoolAdmin->fill($request->all());
        $schoolAdmin->save();
        $roles = Role::whereIn('id', $request->roles)->get();
        $schoolAdmin->syncRoles($roles);
        SchoolAdminLog::Log('编辑管理员: ' . $schoolAdmin->phone);
        return new SchoolAdminResource($schoolAdmin);
    }

    /**
     * @param Request $request
     * @param SchoolAdmin $admin
     * @return SchoolAdminResource|array
     * 重置密码
     */
    public function refreshPassword(Request $request, SchoolAdmin $admin)
    {
        if (!$request->password) return $this->fail('请输入新密码');
        $admin->password = $request->password;
        $admin->save();
        SchoolAdminLog::Log('重置密码: ' . $admin->phone);
        return new SchoolAdminResource($admin);
    }

    /**
     * @param Request $request
     * @return SchoolAdminResource|array
     * 当前用户修改密码
     */
    public function modifyPassword(Request $request)
    {
        $schoolAdmin = SchoolAdmin::where([
            ['id', \Auth::user()->id],
            ['school_id', \Auth::user()->school_id]
        ])->firstOrFail();
        if (!Hash::check($request->oldPassword, $schoolAdmin->password)) {
            return $this->fail('旧密码错误');
        }
        if ($request->password != $request->confirmPassword) {
            return $this->fail('两次输入的密码不一致');
        }
        $schoolAdmin->password = $request->password;
        $schoolAdmin->save();
        SchoolAdminLog::Log('重置密码: ' . $schoolAdmin->phone);
        return new SchoolAdminResource($schoolAdmin);
    }
}
